<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2017/3/23
 * Time: 下午2:06
 */

namespace App\App\Services\Api;

use Illuminate\Support\Facades\Redis;
use \Illuminate\Http\Request;
use \App\Models\ActivityPoints\ActivityPoints;
use \App\App\Services\Web\ActivityService;
use App\App\Services\StorageService;

/**
 * Class PointService 处理埋点数据
 * @package App\App\Services\Api
 */
class PointService
{
    /**
     * @var ActivityPoints 活动埋点模型
     * @package App\Models\ActivityPoints\ActivityPoints
     */
    private $activityPoints;

    /**
     * @var ActivityService 活动的一个服务
     * @package App\App\Services\Web\ActivityService
     */
    private $activityService;

    /**
     * @var Request 客户端请求
     * @package Illuminate\Http\Request
     */
    private $request;

    /**
     * @var Redis redis 连接对象
     */
    private $redis;

    /**
     * @var StorageService 文件存储服务
     */
    private $storageService;

    /**
     * PointService constructor.
     * @param Request $request
     * @param ActivityPoints $activityPoints
     * @param ActivityService $activityService
     * @param StorageService $storageService
     */
    public function __construct(Request $request, ActivityPoints $activityPoints, ActivityService $activityService, StorageService $storageService)
    {
        $this->request = $request;
        $this->activityPoints = $activityPoints;
        $this->activityService = $activityService;
        $this->storageService = $storageService;
        $this->redis = Redis::connection();
    }

    /**
     * 通过埋点key获取埋点中的活动
     *
     * @param $key
     * @return mixed| Array[act_id=>活动id, act_name=>活动名称, img_url=>活动图片, act_url=>活动地址]
     *
     */
    public function get($key)
    {
        $redis = Redis::connection();

        $data = $this->redis->hget('activityDataByPoint', $key);

        if (empty($data)) {
            $data = $this->setCache($key);
        } else {
            $data = json_decode($data, true);
        }

        return $data;
    }


    /**
     * 设置埋点数据到缓存中
     *
     * @param $pointKey 埋点key
     * @return mixed
     */
    public function setCache($pointKey)
    {
        $data = $this->getActivityDataByPoint($pointKey);

        if (empty($data)) {
            return false;
        }

        $res = $this->redis->hset('activityDataByPoint', $pointKey, json_encode($data));

        if ($res) {
            return $data;
        } else {
            return false;
        }
    }


    /**
     * 删除缓存中的埋点数据到缓
     *
     * @param $pointKey
     * @return bool
     */
    public function delCache($pointKey)
    {
        $res = $this->redis->hdel('activityDataByPoint', $pointKey);

        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过埋点获取数据库中的活动地址
     *
     * @param $key 埋点key
     * @return array|bool
     */
    private function getActivityDataByPoint($key)
    {
        $data = array();

        $timestamp = time();

        $activityPoints = $this->activityPoints->where(['point_key' => $key])->with('withActivityDelivery')->first();

        if (empty($activityPoints) || empty($activityPoints->withActivityDelivery)) {
            return false;
        }

        $withActivityDelivery = $activityPoints->withActivityDelivery->where(['status' => 1])->first();

        if (empty($withActivityDelivery)) {
            return false;
        }

        $data['platform'] = json_decode($withActivityDelivery->platform);  //投放平台 1-JS 2-iOS 3-Android
        $data['type'] = $withActivityDelivery->type;  //投放类型 1-banner 2-启动页 3-信息流
        $data['act_id'] = $withActivityDelivery->act_id;  //关联活动id
        $data['act_name'] = $withActivityDelivery->act_name;    //关联活动名称
        $data['img_url'] = $this->storageService->tomedia($withActivityDelivery->img_url); //投放图片
        $data['begin_time'] = $withActivityDelivery->begin_time; //投放图片
        $data['end_time'] = $withActivityDelivery->end_time; //投放图片

        $activity = $this->activityService->findOne($withActivityDelivery->act_id);
        if (empty($activity)) {
            return false;
        }

        $activityModel = $activity->model;
        $host = $this->request->getHttpHost() . '/' . $activityModel->rule . '/activity/index/' . base64_encode(json_encode(['activity_id' => $withActivityDelivery->act_id]));
        $data['act_url'] = 'http://' . $host;
        //            $data['display_type'] = 1;

        return $data;
    }
}
